package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.hfq.UploadBaseData;
import com.ucredit.babel.common.model.hfq.gps.UploadGpsData;
import com.ucredit.babel.common.model.hfq.gps.UploadGpsRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Date;

/**
 * 新接口GPS逆地理解析数据
 *
 * @author guoqiang
 */

public class HfgGpsInfoV2 extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UploadGpsRecord obj = (UploadGpsRecord) model;
        UploadGpsData data = obj.getData();
        UploadBaseData baseData = data.getBaseData();
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(baseData.getHttpTimestamp() == 0 ? "" : DateUtil.formateDateStr(new Date(baseData.getHttpTimestamp() * 1000L))).append(CRT001);
        sb.append(baseData.getUploadInfoOpportunity()).append(CRT001);
        sb.append(baseData.getVersion()).append(CRT001);
        sb.append(baseData.getIsAuthorized()).append(CRT001);
        sb.append(baseData.getOs()).append(CRT001);
        sb.append(data.getLongitude()).append(CRT001);
        sb.append(data.getLatitude()).append(CRT001);
        sb.append(data.getProvince()).append(CRT001);
        sb.append(data.getCity()).append(CRT001);
        sb.append(data.getAddress()).append(CRT001);
        sb.append(data.getAltitude()).append(CRT001);
        sb.append(formatValue(data.getOriginData())).append(CRT001);
        sb.append(formatValue(data.getSematicDescription())).append(CRT001);
        sb.append(formatValue(data.getCityCode())).append(CRT001);

        JSONObject addressComponent = data.getAddressComponent() == null ? new JSONObject() : data.getAddressComponent();
        sb.append(formatValue(addressComponent.getString("country"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("country_code"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("country_code_iso"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("country_code_iso2"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("province"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("city"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("city_level"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("district"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("town"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("town_code"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("distance"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("direction"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("adcode"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("street"))).append(CRT001);
        sb.append(formatValue(addressComponent.getString("street_number"))).append(CRT001);

        StringBuilder TxSb = new StringBuilder(500);
        JSONArray poisData = data.getPois() == null ? new JSONArray() : data.getPois();
        for(Object poisObject: poisData) {
            JSONObject pois = JSONObject.parseObject(JSONObject.toJSONString(poisObject));
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(pois.getString("addr"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("direction"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("distance"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("name"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("poiType"))).append(CRT001);

            String point = pois.getString("point");
            JSONObject jsonPoint = JSON.parseObject(point) == null ? new JSONObject() : JSON.parseObject(point);
            TxSb.append(formatValue(jsonPoint.getString("x"))).append(CRT001);
            TxSb.append(formatValue(jsonPoint.getString("y"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("tag"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("tel"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("uid"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("zip"))).append(CRT001);
            TxSb.append(formatValue(pois.getString("parent_poi"))).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append("pois").append(CRT001);
            key.set(getType() + getSubType());
            value.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, value);
        }
        JSONArray roads = data.getRoads() == null ? new JSONArray() : data.getRoads();
        for(Object roadObject: roads) {
            JSONObject road = JSONObject.parseObject(JSONObject.toJSONString(roadObject));
//            JSONObject road = (JSONObject) roadObject;
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(road.getString("name"))).append(CRT001);
            TxSb.append(formatValue(road.getString("distance"))).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append("roads").append(CRT001);
            key.set(getType() + getSubType());
            value.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, value);
        }
        JSONArray poiRegions = data.getPoiRegions() == null ? new JSONArray() : data.getPoiRegions();
        for(Object poiRegionObject: poiRegions) {
            JSONObject poiRegion = JSONObject.parseObject(JSONObject.toJSONString(poiRegionObject));
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(poiRegion.getString("direction_desc"))).append(CRT001);
            TxSb.append(formatValue(poiRegion.getString("name"))).append(CRT001);
            TxSb.append(formatValue(poiRegion.getString("tag"))).append(CRT001);
            TxSb.append("poiRegion").append(CRT001);
            key.set(getKey());
            value.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, value);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_hfq_gps_v2_inc";
    }

    @Override
    public String getType() {
        return RecordType.HFQ_UPLOAD_GPS_RECORD.toString();
    }

    public String getSubType() {
        return ".gps_info_v2";
    }

    @Override
    public String getComment() {
        return "新接口GPS逆地理解析数据";
    }
}
